/**
 * 
 */
package com.webservice.cliente;

import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.content.SharedPreferences;

import com.fichaClinica.util.Encriptacion;

/**
 * @author MariaJose
 *
 */
public class ClienteWsPrestaciones {

	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL;

	/**
	 * 
	 * @param methodName
	 */

	public static void setAttribute(String methodName) {

		// TODO Auto-generated constructor stub

		METHOD_NAME = methodName;
		NAMESPACE = "http://ws.sgmas.openmrsapi/";
		URL = "http://londres.ceisufro.cl:8080/SGMAS/services/PrestacionesSalud";
	}

	
	/**
	 * 
	 * @param method_name
	 * @param rut
	 * @return
	 */
	public static String ServicioGetPrestacion(String method_name,int idPaciente, int idTipoPrestacion){

		setAttribute( method_name);
		String SOAP_ACTION = NAMESPACE + method_name;
		
		SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
		
		PropertyInfo piIdPrestacion = new PropertyInfo();
		piIdPrestacion.setName("idPrestacion");
		piIdPrestacion.setValue(0);
		piIdPrestacion.setType(int.class);
		
      	Request.addProperty(piIdPrestacion); // 
      	
      	PropertyInfo piIdPaciente = new PropertyInfo();
      	piIdPaciente.setName("idPaciente");
      	piIdPaciente.setValue(idPaciente);
      	piIdPaciente.setType(int.class);
		
      	Request.addProperty(piIdPaciente); 
      	
      	

      	PropertyInfo piIdTipo = new PropertyInfo();
      	piIdTipo.setName("idTipo");
      	piIdTipo.setValue(idTipoPrestacion);
      	piIdTipo.setType(int.class);
		
      	Request.addProperty(piIdTipo); 
      	

		PropertyInfo piIdLocation = new PropertyInfo();
		piIdLocation.setName("idLocation");
		piIdLocation.setValue(0);
		piIdLocation.setType(int.class);

      	Request.addProperty(piIdLocation); // FIJO
      	
		Request.addProperty("username", "4tHy68JOas22L90MBV890FHGVDGJKIkh4g67");
		Request.addProperty("token", "4tHy68JOL90MBV890FHGVDG12233JKIkh4g67");
		Request.addProperty("tokenName", "token_pin");

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.setOutputSoapObject(Request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString = Encriptacion.Decrypt64(resultString.toString(),"wsxedcrfv");
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			datosJsonString = "Error Conexion" + Request.toString();
			return datosJsonString;
		}
	}

	/**
	 * Retorna falso si el rut no existe y verdadero si existe.
	 * @return Boolean
	 */
	public static Boolean rutWebservice(int idPaciente, int idTipoPrestacion, SharedPreferences settings){

		JSONObject jObject;
		try {

			String datosJsonString = ClienteWsPrestaciones.ServicioGetPrestacion("traePrestacion",idPaciente, idTipoPrestacion);

			if(datosJsonString != null){
				//pasa el String a objeto JSON
				jObject = new JSONObject(datosJsonString);				

				/**
				 * se obtienen los valores del json.
				 */
				
//
//				SharedPreferences.Editor editor = settings.edit();
//				editor.putString("run",jObject.getString("run") );
//				editor.putString("nombre", jObject.getString("nombre"));
//				editor.putString("fechanacimiento", jObject.getString("fecha"));
//				editor.putString("idPaciente", jObject.getString("id"));
//				// Commit the edits!
//				editor.commit();

				return true;

			}else{
				return false;
			}
		}
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();  
			return false;
		} 

	}
	
}
